package com.contest.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.contest.dao.WarSituationInfoDao;
import com.contest.entity.UserInfo;
import com.contest.entity.WarSituationInfo;
import com.contest.entity.WarSituationSetup;
import com.contest.service.BettingRecordService;
import com.contest.service.UserInfoService;
import com.contest.service.WarSituationInfoService;
import com.contest.service.WarSituationSetupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * ${table.comment}  服务实现类
 * </p>
 *
 * @author yfl
 * @since ${date}
 */
@Service
public class WarSituationInfoServiceImpl extends ServiceImpl<WarSituationInfoDao, WarSituationInfo> implements WarSituationInfoService {

    @Autowired
    WarSituationInfoDao warSituationInfoDao;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    WarSituationSetupService warSituationSetupService;

    @Override
    public List<WarSituationInfo> selectListByPage(Map<String,Object> map) {
        return warSituationInfoDao.selectByPage(map);
    }

    @Override
    @Transactional
    public boolean updateResult(WarSituationInfo warSituationInfo) {
        List<WarSituationSetup> setups = warSituationSetupService.selectList(null);
        Integer result = warSituationInfo.getFdResult();
        if (1 == result) {
            //发布者胜
            String publishId = warSituationInfo.getFdCreateId();
            UserInfo publish = userInfoService.selectById(publishId);
            float over = publish.getFdOver().floatValue();
            float amount = warSituationInfo.getFdAmount().floatValue()*(2*setups.get(0).getFdFees().floatValue());
            publish.setFdOver(BigDecimal.valueOf((amount * 2) + over));
            userInfoService.updateById(publish);
            warSituationInfo.setFdIsEnd(1);
            warSituationInfo.setFdIsSettlement(1);
        } else if (2 == result) {
            //挑战者胜
            String dareId = warSituationInfo.getFdDareId();
            UserInfo dare = userInfoService.selectById(dareId);
            float amount = warSituationInfo.getFdAmount().floatValue()*(2*setups.get(0).getFdFees().floatValue());
            float over = dare.getFdOver().floatValue();
            dare.setFdOver(BigDecimal.valueOf((amount * 2) + over));
            userInfoService.updateById(dare);
            warSituationInfo.setFdIsEnd(1);
            warSituationInfo.setFdIsSettlement(1);
        } else if (-1 == result) {
            //发布者缺席
            String dareId = warSituationInfo.getFdDareId();
            UserInfo dare = userInfoService.selectById(dareId);
            float amount = warSituationInfo.getFdAmount().floatValue()*(2*setups.get(0).getFdFees().floatValue());
            float over = dare.getFdOver().floatValue();
            dare.setFdOver(BigDecimal.valueOf((amount * 0.3) + over));
            userInfoService.updateById(dare);
            warSituationInfo.setFdIsEnd(1);
            warSituationInfo.setFdIsSettlement(1);
        } else if (-2 == result) {
            //挑战者缺席
            String publishId = warSituationInfo.getFdCreateId();
            UserInfo publish = userInfoService.selectById(publishId);
            float amount = warSituationInfo.getFdAmount().floatValue()*(2*setups.get(0).getFdFees().floatValue());
            float over = publish.getFdOver().floatValue();
            publish.setFdOver(BigDecimal.valueOf((amount * 0.3) + over));
            userInfoService.updateById(publish);
            warSituationInfo.setFdIsEnd(1);
            warSituationInfo.setFdIsSettlement(1);
        } else {
            this.updateById(warSituationInfo);
        }
        this.updateById(warSituationInfo);

        return true;
    }

    @Override
    public int selectByPageCount(Map<String, Object> map) {
        return warSituationInfoDao.selectByPageCount(map);
    }

    /**
     * 用户判定战局
     *
     * @param userInfo
     * @param warId
     * @param result
     * @param pic
     * @return
     */
    @Override
    @Transactional
    public boolean updateResultByUser(UserInfo userInfo, String warId, Integer result, String pic) {
        List<WarSituationSetup> setups = warSituationSetupService.selectList(null);
        WarSituationInfo warSituationInfo = this.selectById(warId);
        String userId = userInfo.getFdId();
        String publishId = warSituationInfo.getFdCreateId();
        String dareId = warSituationInfo.getFdDareId();
        if (userId.equals(publishId)) {
            warSituationInfo.setFdCreateResult(result);
            warSituationInfo.setFdCreatePic(pic);
        } else if (userId.equals(dareId)) {
            warSituationInfo.setFdDarePic(pic);
            warSituationInfo.setFdDareResult(result);
        }

        if(warSituationInfo.getFdDareResult() == null){
            this.updateById(warSituationInfo);
            return true;
        }
        if(warSituationInfo.getFdCreateResult() == null){
            this.updateById(warSituationInfo);
            return true;
        }
        int publishResult = warSituationInfo.getFdCreateResult();
        int dareResult = warSituationInfo.getFdDareResult();
        if (dareResult == 0 && publishResult == 1) {
            float amount = warSituationInfo.getFdAmount().floatValue()*(2*setups.get(0).getFdFees().floatValue());
            UserInfo publish = userInfoService.selectById(warSituationInfo.getFdCreateId());
            float over = publish.getFdOver().floatValue();
            publish.setFdOver(BigDecimal.valueOf((amount * 2) + over));
            userInfoService.updateById(publish);
            warSituationInfo.setFdIsSettlement(1);
            warSituationInfo.setFdResult(1);
        } else if (dareResult == 1 && publishResult == 0) {
            float amount = warSituationInfo.getFdAmount().floatValue()*(2*setups.get(0).getFdFees().floatValue());
            UserInfo dare = userInfoService.selectById(warSituationInfo.getFdDareId());
            float over = dare.getFdOver().floatValue();
            dare.setFdOver(BigDecimal.valueOf((amount * 2) + over));
            userInfoService.updateById(dare);
            warSituationInfo.setFdIsSettlement(1);
            warSituationInfo.setFdResult(2);
        }
        warSituationInfo.setFdIsEnd(1);
        this.updateById(warSituationInfo);
        return true;
    }
}
